package com.dfbz.service;

import com.dfbz.dao.MeetingDao;
import com.dfbz.entity.Meeting;
import com.dfbz.entity.PageEntity;

import java.util.Date;
import java.util.List;

public class MeetService{
    private MeetingDao meetingDao = new MeetingDao();
    public PageEntity<Meeting> findPage(Integer currPage, String title, Integer status) {
        PageEntity<Meeting> pageEntity = new PageEntity<>();
        //页大小
        Integer pageSize=pageEntity.getPageSize();
        //起始索引
        Integer startIndex=(currPage-1)*pageSize;
        //当前页数据
        List<Meeting> data=meetingDao.findByTileAndStatus(title,status,startIndex,pageSize);
        //总数据
        // 总记录数
        Integer totalCount = meetingDao.countByTitleAndStatus(title, status);
        // 总页数
        Integer totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize)+1;

        pageEntity.setCurrPage(currPage);
        pageEntity.setData(data);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setTotalPage(totalPage);
        return pageEntity;
    }

    public void save(Meeting meeting) {
        meeting.setStatus(0L);          //默认状态为0,未开始
        meeting.setPublishDate(new Date());     //发布日期为当前时间
        meetingDao.save(meeting);
    }

    public Meeting findByid(long id) {
        return meetingDao.findByid(id);
    }

    public Integer countJoinMeetingMember(long id) {
        return meetingDao.countJoinMeetingByMeetingId(id);
    }

    public Boolean isJoinMeeting(Long loginUserId, long id) {
        Integer count=meetingDao.isJoinMeeting(loginUserId,id);
        return count==0?false:true;
    }

    public void unJoin(Long loginUserId, long id) {
        meetingDao.unJoin(loginUserId,id);
    }

    public void CanJoin(Long loginUserId, long id) {
        meetingDao.CanJoin(loginUserId,id);
    }
}
